#include <kaleidoscope/frontend/semantic_analysis/environment_creator.hpp>
//=====================================================================
#include <sstream>
//=====================================================================
#include <kaleidoscope/common/symbol_ids.hpp>
#include <kaleidoscope/common/decorate.hpp>
#include <kaleidoscope/frontend/syntactical_analysis/marshall.hpp>
//=====================================================================
using namespace atma::sooty;
using namespace kal;
//=====================================================================

void environment_creator::start_rule(const_parseme_ptr_ref node)
{
	for (parseme_container::const_iterator i = node->children.begin(); i != node->children.end(); ++i)
	{
		if ((*i)->id == ids::function)
			function(*i);
	}
}

//
// 1. int f();
//  -> "F1f%i"
//
//
//
//

void environment_creator::function(const_parseme_ptr_ref node)
{
	// add decorated function to this scope
	std::string decorated_function_name = decorate_function(node);
	value_table().add_binding(marshall::function::name(node)->value.string, node);
	
	// functions are scoping things, so we need to enter the scope
	value_table().enter_scope(marshall::function::name(node)->value.string);
	
	// parameters must be listed inside this aforementioned scope scope
	const_parseme_ptr_ref pl = marshall::function::parameter_list(node);
	for (parseme_container::iterator i = pl->children.begin(); i != pl->children.end(); ++i)
	{
		parameter(*i);
	}
	
	value_table().leave_scope();
}

void environment_creator::parameter(atma::sooty::const_parseme_ptr_ref n)
{
	parseme_ptr_ref name = marshall::parameter::name(n);
	parseme_ptr_ref type = marshall::parameter::type(n);
	
	value_table().add_binding(name->value.string, type);
}


//std::string kal::environment_setup_pass::stringize_type( const_parseme_ptr_ref n )
//{
//	
//}